package com.bds.btcdc.web.service;

import com.bds.btcdc.web.model.StrategyManyArgs;
import org.apache.log4j.Logger;
import org.nutz.dao.Sqls;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.sql.Sql;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by DongChenchen on 2018/5/7 0007
 */
@Service
public class StrategyManyService {
    private Logger LOGGER = Logger.getLogger(getClass());
    @Resource
    private NutDao dao;

    public List getAllByUserId(String user_id){

        Sql sql = Sqls.create("Select a.* from t_strategy_many a where a.user_id=@user_id");
        sql.params().set("user_id", user_id);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(StrategyManyArgs.class));
        dao.execute(sql);
        return sql.getList(StrategyManyArgs.class);
    }

    public StrategyManyArgs getOne(Long user_id,String exchange_code,String en_coin_name){

        Sql sql = Sqls.create("Select a.* from t_strategy_many a where a.user_id=@user_id " +
                "and a.exchange_code=@exchange_code and a.en_coin_name=@en_coin_name");
        sql.params().set("user_id", user_id);
        sql.params().set("exchange_code", exchange_code);
        sql.params().set("en_coin_name", en_coin_name);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(StrategyManyArgs.class));
        dao.execute(sql);
        return sql.getObject(StrategyManyArgs.class);
    }
    public Boolean update(StrategyManyArgs ur){
        if(ur.getId()==null){
            return dao.insert(ur)==null?false:true;
        }else {
            return dao.update(ur) == 1 ? true : false;
        }
    }
    public void test(){
        dao.create(StrategyManyArgs.class,false);
    }

}
